<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/layout/index' }"
        >首页</el-breadcrumb-item
      >
      <el-breadcrumb-item><a>文章管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>文章列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 内容 -->
    <el-card class="box-card">
      <!-- 表格 -->
      <el-table
        height="calc(100vh - 210px)"
        :data="tableData"
        border
        style="width: 100%"
      >
        <!-- 列表索引值，或者使用文档的索引 -->
        <el-table-column v-slot="{ $index }" width="50"
          >{{ $index + 1 }}
        </el-table-column>
        <el-table-column prop="title" label="标题" width="180">
        </el-table-column>
        <el-table-column prop="type" label="类型" width="180">
          <template v-slot="{ row }">{{
            row.type == 1 ? "文章" : "视频"
          }}</template>
        </el-table-column>
        <el-table-column prop="user.nickname" label="作者"> </el-table-column>
        <el-table-column prop="address" label="操作">
          <template v-slot="{ row }">
            <el-button @click="editFn(row.id)">编辑</el-button>
            <el-button type="danger">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 
        分页组件
        @size-change：当pageSize发生改变时触发（每页展示的数据数量发生改变时触发）
        @current-change：当pageIndex发生改变时触发 （页码发生改变时触发）
         -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageIndex"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="5"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { getArticle } from "@/api/article";
export default {
  data() {
    return {
      tableData: [],
      pageIndex: 1, // 当前页数
      pageSize: 5, // 每页条数
      total: 0, // 文章总条数
    };
  },
  created() {
    // 请求获取文章列表
    this.getArticle();
  },

  methods: {
    // 编辑文章
    editFn(id) {
      this.$router.push({
        path: "/layout/articleAdd",
        query: {
          id,
        },
      });
    },
    // 封装获取文章列表
    getArticle() {
      getArticle({
        pageIndex: this.pageIndex,
        pageSize: this.pageSize,
      }).then((res) => {
        console.log(res);
        this.tableData = res.data.data;
        this.total = res.data.total;
      });
    },
    // 点击选择每页条数触发
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.pageIndex = 1;
      // 重新获取数据
      this.getArticle();
    },
    // 点击页码触发
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageIndex = val;
      // 重新获取数据
      this.getArticle();
    },
  },
};
</script>

<style lang="less" scoped>
.el-card {
  margin-top: 20px;
}
</style>